package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * user模块
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping
    public ResponseEntity<List<User>> findAllUser(){
        System.out.println("连接成功");

        //调用业务层
        List<User> users=userService.findAllUser();

        return ResponseEntity.ok(users);
    }

    /**
     * 添加user用户
     * @param user
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveUser(@RequestBody User user){
        userService.saveUser(user);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 批量添加user用户
     * @param userList
     * @return
     */
    @PostMapping("/batch")
    public ResponseEntity<Void> batchSaveUser(@RequestBody List<User> userList){
        userService.batchSaveUser(userList);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
